package com.liuya.safe.policy.controller;

import com.liuya.common.SpringUtils;
import com.liuya.common.WebUtil;
import com.liuya.safe.exception.AuthException;
import com.liuya.safe.model.SafeUserCategory;
import com.liuya.safe.policy.handler.AbstractPolicyDesignHandler;
import com.liuya.safe.policy.handler.UserCategoryPolicyDesignHandler;
import com.liuya.safe.policy.service.UserCategoryManagerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.StringReader;

@Controller
@Api(value = "查询策略", description = "查询策略")
@RequestMapping(value = "userCatePolicyDesign")
public class UserCategoryPolicyDesignController extends AbstractPolicyDesignController {

    // 查询会话
    @ApiOperation(value = "跳转到手工模式主页面")
//    @RequiresPermissions("sys.userCatePolicyDesign.editPolicy")
    @RequestMapping(value = "/editPolicy")
    public String editPolicy(HttpServletRequest req, HttpServletResponse rep, ModelMap modelMap){
        return super.editPolicy(req, rep, modelMap);
    }

    // 查询会话
    @ApiOperation(value = "查询会话")
//    @RequiresPermissions("sys.userCategoryMng.designVariables")
    @RequestMapping(value = "/designVariables")
    public String designVariables(HttpServletRequest req, HttpServletResponse rep, ModelMap modelMap){
        return super.designVariables(req, rep, modelMap);
    }

    // 查询会话
    @ApiOperation(value = "查询会话")
//    @RequiresPermissions("sys.userCategoryMng.designExpr")
    @RequestMapping(value = "/designExpr")
    public String designExpr(HttpServletRequest req, HttpServletResponse rep, ModelMap modelMap){
        return super.designExpr(req, rep, modelMap);
    }

    // 查询会话
    @ApiOperation(value = "查询会话")
//    @RequiresPermissions("sys.userCategoryMng.getVariable")
    @RequestMapping(value = "/getVariable")
    public String getVariable(HttpServletRequest req, HttpServletResponse rep, ModelMap modelMap){
        return super.getVariable(req, rep, modelMap);
    }

    // 查询会话
    @ApiOperation(value = "查询会话")
//    @RequiresPermissions("sys.userCategoryMng.newVariable")
    @RequestMapping(value = "/newVariable")
    public String newVariable(HttpServletRequest req, HttpServletResponse rep, ModelMap modelMap){
        return super.newVariable(req, rep, modelMap);
    }

    // 查询会话
    @ApiOperation(value = "返回设计器主页面")
//    @RequiresPermissions("sys.userCategoryMng.returnMain")
    @RequestMapping(value = "/returnMain")
    public String returnMain(HttpServletRequest req, HttpServletResponse rep, ModelMap modelMap){
        return super.returnMain(req, rep, modelMap);
    }

    // 查询会话
    @ApiOperation(value = "查询会话")
//    @RequiresPermissions("sys.userCategoryMng.exprContextMenu")
    @RequestMapping(value = "/exprContextMenu")
    public String exprContextMenu(HttpServletRequest req, HttpServletResponse rep, ModelMap modelMap){
        return super.exprContextMenu(req, rep, modelMap);
    }

    // 查询会话
    @ApiOperation(value = "查询会话")
//    @RequiresPermissions("sys.userCategoryMng.loadExprForm")
    @RequestMapping(value = "/loadExprForm")
    public String loadExprForm(HttpServletRequest req, HttpServletResponse rep, ModelMap modelMap){
        return super.loadExprForm(req, rep, modelMap);
    }

    // 查询会话
    @ApiOperation(value = "查询会话")
//    @RequiresPermissions("sys.userCategoryMng.deleteVariable")
    @RequestMapping(value = "/deleteVariable")
    @ResponseBody
    public Object deleteVariable(HttpServletRequest req, HttpServletResponse rep, ModelMap modelMap){
        return super.deleteVariable(req, rep, modelMap);
    }

    // 查询会话
    @ApiOperation(value = "查询会话")
//    @RequiresPermissions("sys.userCategoryMng.addVariable")
    @RequestMapping(value = "/addVariable")
    @ResponseBody
    public Object addVariable(HttpServletRequest req, HttpServletResponse rep, ModelMap modelMap){
        return super.addVariable(req, rep, modelMap);
    }

    // 查询会话
    @ApiOperation(value = "查询会话")
//    @RequiresPermissions("sys.userCategoryMng.updateVariable")
    @RequestMapping(value = "/updateVariable")
    @ResponseBody
    public boolean updateVariable(HttpServletRequest req, HttpServletResponse rep, ModelMap modelMap){
        return super.updateVariable(req, rep, modelMap);
    }

    // 查询会话
    @ApiOperation(value = "查询会话")
//    @RequiresPermissions("sys.userCategoryMng.loadExprTree")
    @RequestMapping(value = "/loadExprTree", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String loadExprTree(HttpServletRequest req, HttpServletResponse rep, ModelMap modelMap){
        return super.loadExprTree(req, rep, modelMap);
    }

    // 查询会话
    @ApiOperation(value = "查询会话")
//    @RequiresPermissions("sys.userCategoryMng.addExprChildExprGroup")
    @RequestMapping(value = "/addExprChildExprGroup")
    @ResponseBody
    public boolean addExprChildExprGroup(HttpServletRequest req, HttpServletResponse rep, ModelMap modelMap){
        return super.addExprChildExprGroup(req, rep, modelMap);
    }

    // 查询会话
    @ApiOperation(value = "查询会话")
//    @RequiresPermissions("sys.userCategoryMng.editExprGroup")
    @RequestMapping(value = "/editExprGroup")
    @ResponseBody
    public boolean editExprGroup(HttpServletRequest req, HttpServletResponse rep, ModelMap modelMap){
        return super.editExprGroup(req, rep, modelMap);
    }

    // 查询会话
    @ApiOperation(value = "查询会话")
//    @RequiresPermissions("sys.userCategoryMng.deleteExpr")
    @RequestMapping(value = "/deleteExpr")
    @ResponseBody
    public boolean deleteExpr(HttpServletRequest req, HttpServletResponse rep, ModelMap modelMap){
        return super.deleteExpr(req, rep, modelMap);
    }

    // 查询会话
    @ApiOperation(value = "查询会话")
//    @RequiresPermissions("sys.userCategoryMng.addExprChild")
    @RequestMapping(value = "/addExprChild")
    @ResponseBody
    public boolean addExprChild(HttpServletRequest req, HttpServletResponse rep, ModelMap modelMap){
        return super.addExprChild(req, rep, modelMap);
    }

    // 查询会话
    @ApiOperation(value = "查询会话")
//    @RequiresPermissions("sys.userCategoryMng.editExpr")
    @RequestMapping(value = "/editExpr")
    @ResponseBody
    public boolean editExpr(HttpServletRequest req, HttpServletResponse rep, ModelMap modelMap){
        return super.editExpr(req, rep, modelMap);
    }

    // 查询会话
    @ApiOperation(value = "查询会话")
//    @RequiresPermissions("sys.userCategoryMng.save")
    @RequestMapping(value = "/save")
    @ResponseBody
    public boolean save(HttpServletRequest req, HttpServletResponse rep, ModelMap modelMap){
        return super.save(req, rep, modelMap);
    }

    @RequestMapping(value = "/policyTest")
    public String policyTest(HttpServletRequest req, HttpServletResponse rep, ModelMap modelMap){
        String id = WebUtil.getStringParameter(req, "id", "0");
        return "redirect:/userCatePolicyTest/testPolicy.do?oper=loadFresh&id=" + id;
    }

    @RequestMapping(value = "/policyRaw")
    public String policyRaw(HttpServletRequest req, HttpServletResponse rep, ModelMap modelMap){
        String id = WebUtil.getStringParameter(req, "id", "0");
        return "redirect:/userCatePolicyRaw/editPolicy?oper=loadFresh&id=" + id;
    }

    public String getPolicyHandlerAttributeKey(HttpServletRequest req) {
        String id = WebUtil.getStringParameter(req, "id", "0");
        return "_$safeUserCategory_" + id;
    }

    public AbstractPolicyDesignHandler createPolicyHandler(HttpServletRequest req) {
        String id = WebUtil.getStringParameter(req, "id", "0");

        UserCategoryManagerService manager = (UserCategoryManagerService) SpringUtils.getBean("userCategoryManagerService");
        SafeUserCategory uc = manager.getUserCategory(id);

        com.liuya.db.sql.xml.UserCategoryType xmlUc;
        try {
            xmlUc = com.liuya.db.sql.xml.UserCategory.unmarshal(new StringReader(uc.getXmlContent()));
        } catch (Exception e) {
            throw new AuthException(e);
        }

//        return new UserCategoryPolicyDesignHandler(manager, xmlUc);
        return new UserCategoryPolicyDesignHandler(xmlUc);
    }

    protected void updateHandler(HttpServletRequest req, Object handler){
        String key = getPolicyHandlerAttributeKey(req);
        req.getSession().setAttribute(key, handler);
    }
}
